﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LINQwithListCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> items = new List<string> { "aQua", "RusT", "yElLow", "rEd" };

            var startWithR =
                from item in items
                let uppercasedString = item.ToUpper()   //let 用来重新定义查询范围
                where uppercasedString.StartsWith("R")
                select uppercasedString;

            foreach(var element in startWithR)        //访问list中的某个元素和数组一样 items[i]
            {
                Console.Write("{0}  ", element);
            }//end for

            Console.WriteLine();

            items.Add("Rdf");
            items.Add("sdfg");

            foreach (var element in startWithR)  //LINQ的延迟执行--查询只有在访问结果时才会执行，如果希望立即得到结果可以用LINQ
                                                 //提供的ToArray和ToList方法
            {
                Console.Write("{0}  ", element);
            }//end for 

            Console.ReadKey();
        }//end main
    }
}
